<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/9/7
 * Time: 下午9:44
 */

namespace Home\Controller;

use Think\Controller;
use Think\Page;

class BaseController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        $site_conf = $this->_siteSettings();
        $this->assign('site', $site_conf);
        $this->assign('module_name', strtolower(MODULE_NAME) );
        $this->assign('controller_name', strtolower(CONTROLLER_NAME) );
        $this->assign('action_name', strtolower(ACTION_NAME));
        if(session('current_member'))
        {
            $login_account = session('current_member');
            $this->assign('login_account', $login_account);
        }
    }


    private function _siteSettings()
    {
        if(!S('settings'))
        {
            $data = M('settings')->select();
            foreach($data as $key => $val)
            {
                $k = str_replace('setting_', '', $val['name']);
                $tmp[$k] = unserialize($val['data']);
            }
            S('settings', $tmp, 3600);
        }
        return S('settings');
    }


    /**
     * 封装分页
     * @param $model D('') M('') 实例化模型
     * @param string $where 条件
     * @param string $order 排序规则
     * @param int $page_size 每页记录数
     * @return array
     */
    public function getpage($model, $where = '', $order = '', $page_size = 20)
    {
        $count      = $model->where($where)->count();// 查询满足要求的总记录数
        $Page       = new Page($count, $page_size);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $model->where($where)->order($order)->limit(($Page->firstRow * $Page->listRows).','.$Page->listRows)->select();
        return array(
            'list' => $list,
            'page' => $show,
        );
    }

}